package main

import (
	"encoding/json"
	"fmt"
	"github.com/goinggo/mapstructure"
	"log"
	"reflect"
	"testing"
)

//具体限制条件信息
type StFormRuleInfo struct {
	RuleID    uint32 `json:"RuleID"`
	RuleLimit uint32 `json:"RuleLimit"`
	RuleName  string `json:"RuleName"`
	RuleLabel string `json:"RuleLabel"`
}

type StFormRule struct {
	FormID   uint32            `json:"FormID"`
	FormName string            `json:"FormName"`
	RuleData []*StFormRuleInfo `json:"RuleData"`
}

func JsonToStructDemo2() {
	jsonStr := `[{"FormID":273200001,"FormName":"红包","RuleData":[{"RuleID":3,"RuleLimit":2000,"RuleName":"DAY_BETTING","RuleLabel":"日投注额度"},{"RuleID":2,"RuleLimit":2000,"RuleName":"DAILY_RECHARGE","RuleLabel":"日充值额度"}]}]`
	var people []StFormRule
	err := json.Unmarshal([]byte(jsonStr), &people)
	fmt.Println(err)
	fmt.Println(people)
	for i := range people {
		for j := range people[i].RuleData {
			fmt.Println(people[i].RuleData[j])
		}
	}
}

type Info struct {
	Id int
}

func Log(format string, args ...interface{}) {
	log.Printf(format+"\n", args...)
}
func JsonToMap() {
	//log.SetFlags(log.Ldate | log.Lshortfile) //
	//m := make(map[string]interface{})
	//i := Info{
	//	Id: int(time.Now().UnixNano()),
	//}
	//json1 := jsoniter.ConfigCompatibleWithStandardLibrary
	//data, _ := json.Marshal(i)
	//json1.Unmarshal(data, &m)
	//Log("m is:%+v", m)
	//
	//dec := json1.NewDecoder(bytes.NewBuffer(data))
	//dec.UseNumber()
	//dec.Decode(&m)
	//
	//Log("usenumber m is:%+v", m)
	//dec = json1.NewDecoder(bytes.NewBuffer(data))
	//dec.Decode(&m)
	//Log("m is:%+v", m)
}

func main() {
	// jsong 转结构体
	// JsonToStructDemo()
	// JsonToStructDemo2()

	// json转map
	// JsonToMap()
	MapToStruct()
}

type Person struct {
	Name string
	Age  int
}

// json to struct
func JsonTestStruct2(t *testing.T) {
	jsonStr := `
    {
        "name":"liangyongxing",
        "age":12
    }
    `
	var person Person
	json.Unmarshal([]byte(jsonStr), &person)
	t.Log(person)
}

// struct to json
func TestStruct2Json(t *testing.T) {
	p := Person{
		Name: "liangyongxing",
		Age:  29,
	}

	t.Logf("Person 结构体打印结果:%v", p)

	//Person 结构体转换为对应的 Json
	jsonBytes, err := json.Marshal(p)
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("转换为 json 串打印结果:%s", string(jsonBytes))
}

// json to map
func TestJson2Map(t *testing.T) {
	jsonStr := `
    {
        "name":"liangyongxing",
        "age":12
    }
    `
	var mapResult map[string]interface{}
	//使用 json.Unmarshal(data []byte, v interface{})进行转换,返回 error 信息
	if err := json.Unmarshal([]byte(jsonStr), &mapResult); err != nil {
		t.Fatal(err)
	}
	t.Log(mapResult)
}

// map to json
func TestMap2Json(t *testing.T) {
	mapInstance := make(map[string]interface{})
	mapInstance["Name"] = "liang637210"
	mapInstance["Age"] = 28
	mapInstance["Address"] = "北京昌平区"

	jsonStr, err := json.Marshal(mapInstance)

	if err != nil {
		t.Fatal(err)
	}

	t.Logf("Map2Json 得到 json 字符串内容:%s", jsonStr)
}

type User struct {
	Id       int    `json:"id"`
	Username string `json:"username"`
	Password string `json:"password"`
}

// struct to map
func StructToMap(obj interface{}) map[string]interface{} {
	t := reflect.TypeOf(obj)
	v := reflect.ValueOf(obj)

	var data = make(map[string]interface{})
	for i := 0; i < t.NumField(); i++ {
		data[t.Field(i).Name] = v.Field(i).Interface()
	}
	return data
}

func TestStruct2Map(t *testing.T) {
	user := User{5, "zhangsan", "password"}
	data := StructToMap(user)
	t.Logf("struct2map得到的map内容为:%v", data)
}

// map to struct
func MapToStruct() {
	var mapInstance = make(map[string]interface{})
	mapInstance["Name"] = "liang637210"
	mapInstance["Age"] = 28
	var person Person
	//将 map 转换为指定的结构体
	if err := mapstructure.Decode(mapInstance, &person); err != nil {
		fmt.Println("转换失败", err)
	}
	fmt.Println("map2struct后得到的 struct 内容为:", person)
}
